home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / MISC / COMPAND.ZIP / COMPAND.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-08-02  |  14.7 KB  |  307 lines

  1. ;-----------------------------------------------------------------------;
  2. ; COMPAND.ASM                                                           ;
  3. ; (C) 1993                                                              ;
  4. ; Walla Walla College                                                   ;
  5. ; By: Fred Christensen                                                  ;
  6. ;                                                                       ;
  7. ;                                                                       ;
  8. ; Hardware required:  TI TMS320 DSP Starter Kit (DSK)                   ;
  9. ;    Software files:  COEF.ASM (FIR filter coefficients)                ;
  10. ;                     EXP.ASM (cosine & sine lookup tables)             ;
  11. ;                     DSKA.EXE (Compiler)                               ;
  12. ;                     DSKD.EXE (Debugger interface)                     ;
  13. ;                     README (Operating/Hookup instructions)            ;
  14. ;                                                                       ;
  15. ; Program to input an audio signal using the AIC A/D                    ;
  16. ; converter, compand or decompand the frequencies, and                  ;
  17. ; output it to the AIC D/A converter.                                   ;
  18. ;-----------------------------------------------------------------------;
  19. ;
  20. ;           Memory Management
  21. ; ______________________________________________________________________________
  22. ; B2   | 0060h - 007Fh | Data Memory   | Used by DSKD program
  23. ; B0   | FA00h - FAFFh | Program Memory| Used by DSKD program
  24. ;      | FB00h - FBCFh | Program Memory| Compand program
  25. ;      | FBD0h - FBFFh | Program Memory| Filter1 coefficients
  26. ; B1   | FC00h - FC33h | Program Memory| Filter1 coefficients
  27. ;      | FC34h - FC97h | Program Memory| Filter2 transmit (real) coefficients
  28. ;      | FC98h - FCFBh | Program Memory| Filter2 transmit (imag) coefficients
  29. ;      | FCFCh - FD5Fh | Program Memory| Filter2 receive (real) coefficients
  30. ;      | FD60h - FDC3h | Program Memory| Filter2 receive (imag) coefficients
  31. ;      | FDC4h - FDFFh | Program Memory| Unused
  32. ; B3   | 0600h - 0609h | Data Memory   | Variables used by Compand program
  33. ;      | 06A0h - 06AFh | Data Memory   | Cosine lookup table
  34. ;      | 06B0h - 06BFh | Data Memory   | Sine lookup table
  35. ;      | 06C0h - 079Ah | Data Memory   | Unused
  36. ;      | 079Bh - 07FFh | Data Memory   | Sampling buffer for FIR filtering
  37. ;       
  38. ;-----------------------------------------------------------------------;
  39. ;    Define Global Constants                                            ;
  40. ;-----------------------------------------------------------------------;
  41. YES             .set    1               ;
  42. NO              .set    0               ;
  43. TRANSMIT        .set    1               ;
  44. RECEIVE         .set    0               ;
  45. TR_DEFAULT      .set    TRANSMIT        ; Set transmit/receive flag
  46. NUMTAPS         .set    99              ; Number of filter taps  - 1.
  47. LUPSIZE         .set    16              ; Size of lookup table 
  48.  
  49. ;-----------------------------------------------------------------------;
  50. ;    Setup Memory Mapped Registers (MMRs)                               ;
  51. ;-----------------------------------------------------------------------;
  52. DRR             .set    0       ; Serial port receive register (from A/D)
  53. DXR             .set    1       ; Serial port transmit register (to D/A)
  54. IMR             .set    4       ; Interrupt Mask Register
  55. GREG            .set    5       ; Global memory allocation register
  56.     
  57. ;-----------------------------------------------------------------------;
  58. ;    Analog Interface Circuit (AIC) Constants                           ;
  59. ;                                                                       ;
  60. ;       These are used to configure the TLC32040 AIC for                ;
  61. ;       desired operations.                                             ;
  62. ;-----------------------------------------------------------------------;  
  63.  
  64. AIC_1           .set    03060h  ;TA =RA = 24 0011000001100000 = 03060h
  65. AIC_2           .set    00205h  ;TA'=RA'= 1  0000001000000101 = 00205h
  66. AIC_3           .set    02a56h  ;RB =TB = 21 0010101001010110 = 02a56h 10khz
  67. AIC_CMD         .set    00007h  ;   COMMAND  0000000000000111 = 00007h Filter
  68.  
  69. ;-----------------------------------------------------------------------;
  70. ;    Define Compander Data Memory Variables (All in block B3)           ;
  71. ;-----------------------------------------------------------------------;
  72.  
  73. ;  Note:  The naming convention follows the pseudocode with the addition
  74. ;         of _R or _I  for real or imaginary variables, respectively.
  75.  
  76. ANSR1_R         .set    0600h   ; Real output of Filter #1, temporary variable
  77. ANSR2_R         .set    0601h   ; Real output of Filter #2, temporary variable
  78. ANSR2_I         .set    0602h   ; Imag output of Filter #2, temporary variable
  79. TR_FLAG         .set    0603h   ; Transmit/Receive flag 
  80. EXPPTR_R        .set    0604h   ; Pointer to elements in EXP_R array 
  81. EXPPTR_I        .set    0605h   ; Pointer to elements in EXP_I array 
  82. BUFFER          .set    079Bh   ; Buffer of previous inputs
  83. BUFFER_END      .set    07FEh   ;   Oldest data sample in BUFFER
  84.  
  85. ;-----------------------------------------------------------------------;
  86. ;    Secondary interrupt vector table located in B0 program RAM         ;
  87. ;-----------------------------------------------------------------------;
  88.     .ps     0fa00h          ;     > USERCODE SHOULD NOT OVERWRITE DSKD  <
  89.     b       START           ;RS   > VECTORS.  ON LOAD, INT2 IS RESTORED <
  90.     b       START           ;INT0 > BY DSKD, BUT TRAP IS NOT
  91.     b       START           ;INT1
  92.     b       START           ;INT2  > DSKD LOAD IGNORES INT2 VECTOR
  93.     b       START           ;TINT
  94.     b       RINT            ;RINT  Branch to receive interrupt routine
  95.     eint                    ;XINT  XINT is only for timing, so just return
  96.     ret                     ;
  97.       ; Begin TRAP/DSKD Kernal  ;DSKD load does not restore this code!
  98.  
  99. ;-----------------------------------------------------------------------;
  100. ;    Application code is located after DSKD kernal                      ;
  101. ;-----------------------------------------------------------------------;
  102.     .ps     0fb00h          ;
  103.     .entry                  ;
  104. START:  spm     0               ; Sets P shift mode to no shift
  105.     ldpk    0               ; Set direct addressing to MMRs and B2
  106.     fort    0               ; Serial port : 16 bit
  107.     rtxm                    ;             : ext. FSX
  108.     sfsm                    ;             ; burst mode
  109.     lack    080h            ; AIC reset by pulsing /BR (Global Data)
  110.     sach    DXR             ; send 0 to DXR (AIC)
  111.     sacl    GREG            ; 256 * 100 nS /BR pulse
  112.     lrlk    AR0,0FFFFh      ;
  113.     rptk    255             ; read junk from address 0xFFFF
  114.     lac     *,0,AR0         ;
  115.     conf    2               ; B0,B3 as DRAM if direct bootload
  116.  
  117.                 ;  ***** INITIALIZE *****
  118.     
  119.     ldpk    12              ; Set addressing to compander data memory
  120.     lack    TR_DEFAULT      ; Load & store transmit/receive status
  121.     sacl    TR_FLAG         ; 
  122.     lrlk    AR5,EXP_R       ; Load & store pointer cosine 
  123.     sar     AR5,EXPPTR_R    ;    lookup table       
  124.     lrlk    AR6,EXP_I       ; Load & store pointer to sine
  125.     sar     AR6,EXPPTR_I    ;    lookup table       
  126.     ldpk    0               ; Set direct addressing to MMRs and B2  
  127.     sovm                    ; catch accumulator overflows
  128.  
  129. ;-----------------------------------------------------------------------;       
  130. ;    Load the configuration commands into the AIC                       ;
  131. ;-----------------------------------------------------------------------;
  132. AIC_RS  lack    024h            ; Turn on XINT
  133.     sacl    IMR             ;
  134.     idle                    ;
  135.     lalk    AIC_1           ; Load each AIC configuration word
  136.     call    AIC_2nd         ; and load it into the AIC
  137.     lalk    AIC_2           ;
  138.     call    AIC_2nd         ;
  139.     lalk    AIC_3           ;
  140.     call    AIC_2nd         ;
  141.     lalk    AIC_CMD         ;
  142.     call    AIC_2nd         ;
  143.     ssxm                    ;
  144.     lack    014h            ; AIC RINT
  145.     sacl    IMR             ; where INT0 indicates EOC (End Of Conv)
  146.  
  147. ;-----------------------------------------------------------------------;
  148. ;    Main program loop                                                  ;
  149. ;-----------------------------------------------------------------------;
  150. LOOPING idle                    ; Infinite loop waiting for interrupts
  151.     b       LOOPING  
  152.  
  153. ;-----------------------------------------------------------------------;
  154. ;    RINT Interrupt handler                                             ;
  155. ;-----------------------------------------------------------------------;
  156. RINT:   dint                    ; disable interrupts
  157.  
  158.                 ;  ***** INPUT *****
  159.  
  160.     ldpk    12              ; Set addressing to compander data memory
  161.     lar     AR5,EXPPTR_R    ; Load cosine lookup table pointer
  162.     lar     AR6,EXPPTR_I    ; Load sine lookup table pointer        
  163.     ldpk    0               ; Set addressing to MMRs and B2
  164.     lac     DRR,0           ; Load ACC with A/D from AIC
  165.     ldpk    15              ; Set addressing to filter buffer
  166.     sacl    BUFFER          ; Store data in first filter buffer location
  167.  
  168.                 ;  ***** FILTER #1 *****
  169.  
  170.     call    FIRINIT         ; FIR initialization 
  171.     larp    AR7             ;
  172.     rptk    NUMTAPS         ; Compute FIR output for filter #1 
  173.     mac     COEF1_R,*-,AR7  ;
  174.     apac                    ;
  175.     ldpk    12              ; Set addressing to compander data memory 
  176.     sach    ANSR1_R,0       ; Divide by 2^16 and store.
  177.  
  178.                 ;  ***** FILTER #2 *****
  179.  
  180.     lac     TR_FLAG         ; Load Receive/Transmit flag
  181.     bz      RECV            ; Branch if receive mode (TR_FLAG=0)
  182. TRANS   call    FIRINIT         ; FIR initialization
  183.     rptk    NUMTAPS         ; Compute FIR output for filter #2 (real) 
  184.     mac     COEF2T_R,*-,AR7 ;
  185.     apac                    ;
  186.     ldpk    12              ; Set addressing to compander data memory 
  187.     sach    ANSR2_R,0       ; Divide by 2^16 and store.
  188.  
  189.     call    FIRINIT         ; FIR initialization
  190.     rptk    NUMTAPS         ; Compute FIR output for filter #2 (imag)
  191.     macd    COEF2T_I,*-     ;    with buffer shift
  192.     apac                    ;
  193.     ldpk    12              ; Set addressing to compander data memory
  194.     sach    ANSR2_I,0       ; Divide by 2^16 and store.     
  195.     b       SHIFT           ; Branch to frequency shifting routine
  196.  
  197. RECV    call    FIRINIT         ; FIR initialization
  198.     rptk    NUMTAPS         ; Compute FIR output for filter #2 (real) 
  199.     mac     COEF2R_R,*-,AR7 ;
  200.     apac                    ;
  201.     ldpk    12              ; Set addressing to compander data memory 
  202.     sach    ANSR2_R,0       ; Divide by 2^16 and store.
  203.  
  204.     call    FIRINIT         ; FIR initialization
  205.     rptk    NUMTAPS         ; Compute FIR output for filter #2 (imag)
  206.     macd    COEF2R_I,*-     ;    with buffer shift
  207.     apac                    ;
  208.     ldpk    12              ; Set addressing to compander data memory
  209.     sach    ANSR2_I,0       ; Divide by 2^16 and store
  210.  
  211.                 ;  ***** SHIFT *****
  212.  
  213. SHIFT   zac                     ; Zero accumulator
  214.     lt      ANSR2_R,0       ; Multiply output (real part) of Filter #2      
  215.     larp    AR5
  216.     mpy     *+,AR6          ;    by cos(2*pi*fo*t)
  217.     apac
  218.     sach    ANSR2_R,0       ; Divide by 2^16 and store result
  219.     zac                     ; Zero ACC
  220.     lt      ANSR2_I,0       ; Multiply output (imag part) of Filter #2
  221.     mpy     *+,AR0          ;    by sin(2*pi*fo*t)
  222.     apac
  223.     sach    ANSR2_I,0       ; Divide by 2^16 and store result
  224.     
  225.     lrlk    AR0,EXP_R       ; Check if lookup pointers are at end
  226.     adrk    LUPSIZE         ;    of lookup tables.  If so, then 
  227.     larp    AR5             ;    reload them to point to beginning
  228.     cmpr    0               ;   
  229.     bbz     SHIFTUD         ;
  230.     lrlk    AR5,EXP_R       ;
  231.     lrlk    AR6,EXP_I       ;
  232.  
  233. SHIFTUD lac     TR_FLAG         ; Test if Receive or Transmit
  234.     bnz     SHIFTDN         ; If Transmit then branch
  235. SHIFTUP lac     ANSR2_R,0       ; Shift band up
  236.     add     ANSR2_I,0       ; 
  237.     b       SUM
  238. SHIFTDN lac     ANSR2_I,0       ; Shift band down 
  239.     neg
  240.     add     ANSR2_R,0       ; 
  241.  
  242.                 ;  ***** COMBINE *****
  243.  
  244. SUM     add     ANSR1_R,0       ; Add filt#1 to shifted filt#2 output
  245.  
  246.                 ;  ***** OUTPUT *****
  247.  
  248. OUT     ldpk    0               ; Set addressing to MMRs and B2
  249.     andk    0fffch          ; Prepare word for transmit to AIC
  250.     sacl    DXR,0           ; Output ACC to AIC for D/A
  251.     ldpk    12              ; Set addressing to compander data memory
  252.     sar     AR5,EXPPTR_R    ; Store cosine table lookup pointer
  253.     sar     AR6,EXPPTR_I    ; Store sine table lookup pointer       
  254.     eint                    ; enable interrupts
  255.     ret                     ;
  256.  
  257.  
  258.  
  259.  
  260. ;-----------------------------------------------------------------------;
  261. ;    Routine which sends a signal to the AIC requesting permission      ;
  262. ;    to send the configuration commands to it.                          ;
  263. ;-----------------------------------------------------------------------;
  264. AIC_2nd adlk    6,15            ; set ACCU_hi = 3 for secondary XMIT
  265.     idle                    ; Wait for a XINT
  266.     sach    DXR             ;
  267.     idle                    ; ACCU_hi requests 2nd XMIT
  268.     sacl    DXR             ;
  269.     idle                    ; ACCU_lo sets up registers
  270.     sacl    DXR,2           ; close command with LSB = 00
  271.     idle                    ;
  272.     eint                    ;
  273.     ret                     ;
  274.  
  275. ;-----------------------------------------------------------------------;
  276. ;    Routine to initialize setup for FIR computations                   ;
  277. ;-----------------------------------------------------------------------;
  278. FIRINIT zac                     ; Zero Accumulator
  279.     mpyk    0               ; Zero product register
  280.     ldpk    15              ; Set addressing to filter buffer memory
  281.     lrlk    AR7,BUFFER_END  ; Load AR7 with end of buffer location
  282.     ret
  283.  
  284.  
  285.  
  286. ;-----------------------------------------------------------------------;
  287. ;  The following filter coefficients were obtained from Matlab,         ;
  288. ;  multiplied by (2^15 - 1) and rounded to the nearest integer value.   ;
  289. ;  See Matlab file for an explanation on how to compute these.          ;
  290. ;-----------------------------------------------------------------------;
  291.     .include        "COEF.ASM"  ; Filter coefficients
  292.  
  293.  
  294. ;-----------------------------------------------------------------------;
  295. ;  Include the EXP lookup tables.  Note that:                           ;
  296. ;      Re( exp(-j*2*pi*fo*t) )  =  cos(2*pi*fo*t)                       ;
  297. ;      Re( exp(-j*2*pi*fo*t) )  =  cos(2*pi*fo*t)                       ;
  298. ;  These were generated in Matlab and the number of elements in these   ; 
  299. ;  tables is a function of the sampling rate and frequency shift (fo).  ;
  300. ;-----------------------------------------------------------------------;
  301.     .ds             06a0h 
  302.     .include        "EXP.ASM"   ; Exponential lookup tables
  303.  
  304.  
  305.  
  306.  
  307.